#include "head.h"

int recvNameSize(int sockfd,off_t *fileSize){

    //接收文件名称
    train_t train;
    recvn(sockfd,&train.length,sizeof(int));
    recvn(sockfd,train.data,train.length);
    char fileName[4096] = {0};
    memcpy(fileName,train.data,train.length);
    //接收文件大小
    bzero(&train,sizeof(train_t));
    recvn(sockfd,&train.length,sizeof(int));
    recvn(sockfd,train.data,train.length);
    memcpy(&fileSize,train.data,sizeof(off_t));
    printf("FileSize = %ld\n",*fileSize);
    return 0;
}

//int recvFile(int sockfd,char *fileName){
//    printf("Start Download\n");
//    ////接收文件名称
//    //int fileExist = 0;
//    //train_t train;
//    //recvn(sockfd,&train.length,sizeof(int));
//    //recvn(sockfd,train.data,train.length);
//    //char fileName[4096] = {0};
//    //memcpy(fileName,train.data,train.length);
//    ////接收文件大小
//    //off_t fileSize;
//    //bzero(&train,sizeof(train_t));
//    //recvn(sockfd,&train.length,sizeof(int));
//    //recvn(sockfd,train.data,train.length);
//    //memcpy(&fileSize,train.data,sizeof(off_t));
//    //printf("FileSize = %ld\n",fileSize);
//
//    //判断文件是否存在
//    int fileExist = 0;
//    train_t train;
//    off_t fileSize;
//    isExist(fileName,&fileExist);
//    if(fileExist == 1){
//        
//        ////文件存在，断点续传
//        int fd = open(fileName,O_RDWR|O_CREAT,0666);
//        struct stat statbuf;
//        fstat(fd,&statbuf);
//        //发文件偏移量
//        bzero(&train,sizeof(train_t));
//        memcpy(train.data,&statbuf.st_size,sizeof(off_t));
//        train.length = sizeof(off_t);
//        send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
//        off_t existFileSize = statbuf.st_size;
//        //接收文件名和大小
//        recvNameSize(sockfd,&fileSize);
//
//        if(existFileSize == fileSize){
//            printf("File complete download\n");
//            return 0;
//        }
//        else{
//            printf("Client file len: %ld,Server file len: %ld\n",
//                   existFileSize,fileSize);
//            printf("Start Download Again\n");
//            ftruncate(fd,fileSize);
//            lseek(fd,existFileSize,SEEK_SET);
//        
//        }
//    }
//    else{
//
//        ////下载文件，接收文件内容
//        //int fd = open(fileName,O_RDWR|O_CREAT,0666);
//        //ftruncate(fd,fileSize);
//        //char *p = (char *)mmap(NULL,fileSize,PROT_READ|PROT_WRITE,MAP_SHARED,
//        //                       fd,0);
//        //int ret = recvn(sockfd,p,fileSize);
//        //ERROR_CHECK(ret,-1,"file_recvn");
//        //printf("File complete download\n");
//        //munmap(p,fileSize);
//        //close(fd);
//    }
//    return 0;
//}

int sendFile(int sockfd,char *fileName){
    printf("Start Upload\n");
    //发送文件名
    train_t train;
    bzero(&train,sizeof(train_t));
    strcpy(train.data,fileName);
    train.length = strlen(train.data);
    int ret = send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    ERROR_CHECK(ret,-1,"send_file");

    int fd = open(fileName,O_RDWR);
    ERROR_CHECK(fd,-1,"open");
    //发送文件大小
    struct stat statbuf;
    fstat(fd,&statbuf);
    memcpy(train.data,&statbuf.st_size,sizeof(off_t));
    train.length = sizeof(off_t);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    //MMAP零拷贝技术发送文件内容
    char *p = (char *)mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,
                           MAP_SHARED,fd,0);
    send(sockfd,p,statbuf.st_size,MSG_NOSIGNAL);
    munmap(p,statbuf.st_size);

    printf("Upload Success\n");
    return 0;
}

int is_file_exist_tmp(char *fileName){

    return 0;
}

int recvFile(int sockfd,char *fileName){
    //char file_name[256] = {0};
    //int file_len;
    ////接收文件名
    //recvn(sockfd,(char *)&file_len,sizeof(int));
    //recvn(sockfd,file_name,file_len);
    //if(1 == (is_file_exist_tmp(file_name))){
    //    //如果
    //}

    return 0;
}
